/**
 * created by 小卷毛, 2022-07-23
 * Copyright (c) 2022, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.rzico.sbl.viewmodel

import android.os.Parcelable
import com.lzg.extend.BaseResponse
import com.lzg.extend.JsonConvert
import com.lzg.extend.jackson.JacksonConvert
import com.lzg.okrx.adapter.FlowableBody
import com.lzy.okgo.OkGo
import com.rzico.sbl.model.*
import com.xinnuo.common.helper.getTimeString
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.utils.applyFlowableSchedulers
import com.xinnuo.common_ui.utils.subscribeByFinally

/**
 * 描 述：
 * 作 者：小卷毛  2022-07-23 10:42
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
class PickupViewModel : DialogViewModel() {

    val mTypeList by lazy {
        listOf(
            FilterData("云柜-普通版", "AIR_BASIC", "云柜-普通版"),
            FilterData("云柜-普通扩展版", "AIR_BASIC_EXTEND", "云柜-普通扩展版"),
            FilterData("云柜-触屏版", "AIR_TOUCh", "云柜-触屏版"),
            FilterData("云柜-触屏扩展版", "AIR_TOUCh_EXTEND", "云柜-触屏扩展版")
        )
    }

    val mStatusList by lazy {
        listOf(
            FilterData("在线", "online", "在线"),
            FilterData("离线", "offline", "离线"),
            FilterData("停用", "stop", "停用")
        )
    }

    /**
     * 自提站点列表
     */
    fun stationList(
        page: Int,
        keyWord: String,
        categoryId: String,
        pageSize: Int = 10,
        isLoading: Boolean = false,
        onSuccess: (ArrayList<PickData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.post<BaseResponse<ResponseModel<PickData>>>(BaseUrl.mngPick)
            .params(
                getParams(
                    "searchValue" to keyWord,
                    "categoryId" to categoryId,
                    "pageStart" to page * pageSize,
                    "pageSize" to pageSize
                )
            )
            .isMultipart(true)
            .converter(object : JsonConvert<BaseResponse<ResponseModel<PickData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .doOnSubscribe { if (isLoading) showLoading() }
            .doFinally { if (isLoading) dismissLoading() }
            .map { it.data.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 自提点分类列表
     */
    fun categoryList(
        show: String,
        onSuccess: (ArrayList<PickCategory>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ArrayList<PickCategory>>>(BaseUrl.mngPickCate)
            .params(
                getParams(
                    "type" to "dummy",
                    "show" to show
                )
            )
            .converter(object : JacksonConvert<BaseResponse<ArrayList<PickCategory>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }
            .map { it.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 自提站点商品列表
     */
    fun goodsList(
        page: Int,
        keyWord: String,
        shopId: String,
        pageSize: Int = 10,
        onSuccess: (ArrayList<PickGoodsData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.post<BaseResponse<ResponseModel<PickGoodsData>>>(BaseUrl.mngPickPro)
            .params(
                getParams(
                    "searchValue" to keyWord,
                    "shopId" to shopId,
                    "pageStart" to page * pageSize,
                    "pageSize" to pageSize
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<PickGoodsData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 自提站点商品统计列表
     */
    fun reportList(
        page: Int,
        shopId: String,
        cupboardId: String,
        beginDate: String,
        endDate: String,
        pageSize: Int = 10,
        onSuccess: (ResponseModel<PickReportData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ResponseModel<PickReportData>>>(BaseUrl.mngPickRep)
            .params(
                getParams(
                    "shopId" to shopId,
                    "cabinetId" to cupboardId,
                    "beginDate" to beginDate,
                    "endDate" to endDate,
                    "pageStart" to page * pageSize,
                    "pageSize" to pageSize
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<PickReportData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 自提站点订单统计列表
     */
    fun reportOrder(
        page: Int,
        keyWord: String,
        shopId: String,
        cupboardId: String,
        beginDate: String,
        endDate: String,
        pageSize: Int = 10,
        onSuccess: (ResponseModel<Parcelable>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.post<BaseResponse<ResponseModel<OrderData>>>(BaseUrl.mngPickOrd)
            .params(
                getParams(
                    "searchValue" to keyWord,
                    "shopId" to shopId,
                    "cabinetId" to cupboardId,
                    "beginDate" to beginDate,
                    "endDate" to endDate,
                    "pageStart" to page * pageSize,
                    "pageSize" to pageSize
                )
            )
            .isMultipart(true)
            .converter(object : JsonConvert<BaseResponse<ResponseModel<OrderData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { data ->
                val model = ResponseModel<Parcelable>()
                model.summary = data.data.summary

                ArrayList<Parcelable>().apply {
                    data.data.data.forEach { item ->
                        // 添加头部
                        add(OrderHeader().apply {
                            orderSN = item.sn
                            hint = "下单时间："
                            time = item.createDate.getTimeString()
                            status_hint = item.statusDescr
                        })

                        // 添加商品
                        addItems(
                            item.orderItems.onEach {
                                it.orderSN = item.sn
                                it.consignee = item.consignee.orEmpty()
                                it.phone = item.phone.orEmpty()
                            }
                        )

                        // 添加底部
                        add(OrderFooter().apply {
                            orderSN = item.sn
                            total_num = item.quantity
                            amount = item.amountPaid.orEmpty()
                        })
                    }

                    model.data = this
                }

                model
            }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 自提站点自提柜列表
     */
    fun cupboardList(
        page: Int,
        keyWord: String,
        shopId: String,
        pageSize: Int = 10,
        isLoading: Boolean = false,
        onSuccess: (ArrayList<CupboardData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ResponseModel<CupboardData>>>(BaseUrl.mngCupList)
            .params(
                getParams(
                    "shopId" to shopId,
                    "searchValue" to keyWord,
                    "pageStart" to page * pageSize,
                    "pageSize" to pageSize
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<CupboardData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .doOnSubscribe { if (isLoading) showLoading() }
            .doFinally { if (isLoading) dismissLoading() }
            .map { it.data.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 自提柜门列表
     */
    fun cupboardItem(
        cupboardId: String,
        isLoading: Boolean = false,
        onSuccess: (ArrayList<CupboardCodeData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ArrayList<CupboardCodeData>>>(BaseUrl.mngCupCell)
            .params(getParams("id" to cupboardId))
            .converter(object : JacksonConvert<BaseResponse<ArrayList<CupboardCodeData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .doOnSubscribe { if (isLoading) showLoading() }
            .doFinally { if (isLoading) dismissLoading() }
            .map { it.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 自提柜缺货统计
     */
    fun cupboardSummary(
        keyWord: String,
        shopId: String,
        onSuccess: (ArrayList<CupboardSummary>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.post<BaseResponse<ArrayList<CupboardSummary>>>(BaseUrl.mngCupFillSum)
            .params(
                getParams(
                    "shopId" to shopId,
                    "searchValue" to keyWord,
                )
            )
            .converter(object : JacksonConvert<BaseResponse<ArrayList<CupboardSummary>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

}